<?php 
// +----------------------------------------------------------------------
// | Item        
// +----------------------------------------------------------------------
// | Copyright
// +----------------------------------------------------------------------
// | Versions
// +----------------------------------------------------------------------
// | Author       YanHao
// +----------------------------------------------------------------------
// | Date         2017-07-07
// +----------------------------------------------------------------------

namespace app\index\controller;

use app\auth\logic\AuthEncrypt; 
use app\auth\model\User;
use think\Request;

class Base
{
    const AUTH_TOKEN = 'token';

    protected $uid;
    protected $request;

    public function __construct(Request $request = null)
    {
        if (null === $request) {
            $request = Request::instance();
        }
        $this->request = $request;
        if ($request->has(self::AUTH_TOKEN)) {
            $token = $request->get(self::AUTH_TOKEN);
            list($uid, $authKey) = AuthEncrypt::decrypt($token);
            if (User::validateAuth($uid, $authKey)) {
                $this->uid = $uid;
            } else {
              return json(['code'=>0, 'msg'=>'no access']);
            }
        }
    }

}